package com.zzj.ecology.system.service.impl.system;

import com.zzj.ecology.common.constant.GlobalConstant;
import com.zzj.ecology.system.bean.system.SysPost;
import com.zzj.ecology.system.bean.system.SysPostExample;
import com.zzj.ecology.system.dto.HttpResult;
import com.zzj.ecology.system.domain.Page;
import com.zzj.ecology.system.mapper.system.SysPostMapper;
import com.zzj.ecology.system.service.system.SysPostService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName SysPostServiceImpl
 * @Description TODO
 * @Author zhijun zeng at 23:44 2020/4/9
 * @Version 1.0
 **/
@Service("sysPostService")
@Transactional(rollbackFor = Exception.class)
public class SysPostServiceImpl implements SysPostService {

    public static final Logger logger = LoggerFactory.getLogger(SysPostServiceImpl.class);

    @Autowired
    private SysPostMapper sysPostMapper;


    /**
     * 分页查询岗位信息数据
     * @param page
     * @return
     */
    public HttpResult findPageList(Page page) {
        int currentPage = page.getCurrentPage();
        Integer pageCounts = page.getPageCounts();
        int first = (currentPage - 1) * pageCounts;

        List<SysPost> lists = sysPostMapper.findPageList(first, pageCounts);
        page.getData().put(HttpResult.LIST_TAG, lists);// 当前页面数据

        SysPostExample example = new SysPostExample();
        SysPostExample.Criteria criteria = example.createCriteria();
        criteria.andDeleteStatusEqualTo(GlobalConstant.DELETE_STATUS_NOT_DELETE);//delete_status='0' 的数据

        List<SysPost> list1 = sysPostMapper.selectByExample(example);//不分页总数据
        page.setTotalCounts(list1.size());
        return HttpResult.success().add(HttpResult.PAGE_TAG, page);

    }

    /**
     * 查询所有 岗位 列表
     * @return
     */
    public HttpResult findPostSelectBoxList() {
        SysPostExample example = new SysPostExample();
        example.createCriteria().andDeleteStatusEqualTo(GlobalConstant.DELETE_STATUS_NOT_DELETE);
        example.setOrderByClause("post_id asc");
        List<SysPost> list = sysPostMapper.selectByExample(example);
        return HttpResult.success().add(HttpResult.DATA_TAG, list);
    }

    /**
     * 查询 岗位名称 是否存在
     * @param postName
     * @return
     */
    public boolean checkExists(String postName) {
        SysPostExample example = new SysPostExample();
        example.createCriteria().andPostNameEqualTo(postName);
        List<SysPost> posts = sysPostMapper.selectByExample(example);
        return posts.size() > 0 ? true : false;
    }

    /**
     * 创建一条新的岗位信息
     * @param sysPost
     * @return
     */
    public HttpResult create(SysPost sysPost) {
        sysPostMapper.insertSelective(sysPost);
        return HttpResult.success();
    }

    /**
     * 根据 Post 岗位信息的 ID 修改 岗位信息
     * @param sysPost
     * @return
     */
    public HttpResult update(SysPost sysPost) {
        int i = sysPostMapper.updateByPrimaryKeySelective(sysPost);
        return i > 0 ? HttpResult.success() : HttpResult.error();
    }

    /**
     * 批量删除 批量更新 delete_status='0' -> delete_status='1' 标识删除
     *
     * 注意: 使用了update ,不出现问题. 
     *
     * @param ids
     * @param username
     * @return
     */
    public HttpResult updateDeleteStatusByIds(List<Integer> ids, String username) {
        int i = sysPostMapper.updateDeleteStatusByIds(ids, username);
        return i>0 ? HttpResult.success() : HttpResult.error();
    }


}
